//双重检查锁定的懒汉模式

class SingletonLazy {
    private SingletonLazy() {}

    private static volatile SingletonLazy instance = null;

    public static SingletonLazy getInstance() {
        // instance 如果为 null, 说明是首次调用, 就需要加锁. 如果非 null, 就说明是后续调用, 就不用加锁了.
        if (instance == null) {
            synchronized (SingletonLazy.class) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
}

public class Demo19 {
    public static void main(String[] args) {
        SingletonLazy singletonLazy1 = SingletonLazy.getInstance();
        SingletonLazy singletonLazy2 = SingletonLazy.getInstance();

        if (singletonLazy1 == singletonLazy2) {
            System.out.println("同一个instance对象");
        }
    }
}
